/**
 *
 */
package org.tangram.component.base;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.tangram.component.Component;
import org.tangram.component.ComponentManager;
import org.tangram.component.ComponentState;
import org.tangram.component.xml.ComponentDefintion;
import org.tangram.component.xml.DependencyDefintion;

/**
 * @author han_feng
 * 
 */
public class BaseComponent implements Component {

	private String groupId;
	private String id;
	private String version;
	private String name;
	private String description;

	private List<Component> dependencies;

	private ComponentState state = ComponentState.VALID;

	private ComponentDefintion defintion;

	public ComponentDefintion getDefintion() {
		return defintion;
	}

	public BaseComponent(String groupId, String id, String version) {
		this.groupId = groupId;
		this.id = id;
		this.version = version;
	}

	public BaseComponent(ComponentDefintion defintion) {
		this.defintion = defintion;
		this.groupId = defintion.getGroupId();
		this.id = defintion.getId();
		this.version = defintion.getVersion();
		this.name = defintion.getName();
		this.description = defintion.getDescription();
	}

	/**
	 * @return the descriptorURL
	 */
	public URL getDescriptorURL() {
		return defintion.getDescriptorURL();
	}

	/**
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;

		if (obj == this)
			return true;

		if (obj instanceof BaseComponent) {
			BaseComponent bc = (BaseComponent) obj;
			return bc.getKey().equals(this.getKey());
		}

		return super.equals(obj);
	}

	/**
	 * @return the dependencies
	 */
	public List<Component> getDependencies() {
		if (dependencies == null) {
			dependencies = new ArrayList<Component>();
			ComponentManager manager = ComponentManager
					.getDefaultComponentManager();
			Component component;

			for (DependencyDefintion depend : defintion
					.getDependencyDefintions()) {
				component = manager.getComponent(depend.getGroupId(),
						depend.getId());
				if (component != null)
					dependencies.add(component);
			}
		}

		return dependencies;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the groupId
	 */
	public String getGroupId() {
		return groupId;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	public String getKey() {
		return this.getGroupId() + ":" + this.getId();
	}

	/**
	 * @return the state
	 */
	public ComponentState getState() {
		return state;
	}

	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return getKey().hashCode();
	}

	/**
	 * @param state
	 *            the state to set
	 */
	public void setState(ComponentState state) {
		this.state = state;
	}

	public String toString() {
		return getKey() + "|" + getState().toString() + "|" + getDescription()
				+ "|" + getDescriptorURL();
	}

}
